tests = {
	jsLocation: "../../dunit/trunk/dreamunit/js/runtime/dreamunit_1_0.js",
	list: [],
	loadForSet: function(suid) {
		tests.list = [];
		tests.deserializeTestsFromFileSystem(suid);
	},
	syncTests: function(uid) {
		// first we try to contact the internet.
		// if no conneciton then we just use the local copy.
		var result = remoteData.requestData(ws.DOWNLOADSET, {'uid':uid}, "");
		if (result.replace(/^\s+|\s+$/g,"") != "") {
			
		} 
		
		// Now load them from the file system
	},
	updateTest: function(uid) {
		var tIndex = tests.getTestIndexByUID(uid);
		if (tests.list[tIndex].appliestoroot && tests.list[tIndex].appliestoroot.length > 0) {
			tests.list[tIndex].appliestoroot = tests.list[tIndex].appliestoroot.replace("\\\\", "\\");
		}
		var result = remoteData.interpretServerBoolean(remoteData.postData(ws.UPDATETEST, {'uid':uid, 'testname':tests.list[tIndex].name, 'appliesto':tests.list[tIndex].appliesto,'appliestoroot':tests.list[tIndex].appliestoroot}, {'js':tests.list[tIndex].js}));
		tests.serializeAndWriteTestsToFileSystem(tests.list[tIndex].suid);
	},
	getTestIndexByUID: function(uid) {
		var res = 0;
		for (var i = 0; i < tests.list.length; i++) {
			if (tests.list[i].uid == uid) {
				res = i;
			}
		}
		return res;		
	},
	getTestByUID: function(uid) {
		var res = tests.list[0];
		for (var i = 0; i < tests.list.length; i++) {
			if (tests.list[i].uid == uid) {
				res = tests.list[i];
			}
		}
		return res;
	},	
	createNew: function(suid,name) {
		var rootfolder = site.siteRelativeToLocalPath("/");
		var nUid = generateUID();
		var result = remoteData.interpretServerBoolean(remoteData.requestData(ws.CREATETEST, {'suid':suid,'uid':nUid, 'testname':name, 'appliesto':'','appliestoroot':rootfolder}));
		tests.list[tests.list.length] = tests.createTestObject(nUid, suid,name, "");
		tests.serializeAndWriteTestsToFileSystem(suid);
		return nUid;
	},
	serializeAndWriteTestsToFileSystem: function(suid) {
		var fname = tests.getTestsFileLocation(suid);
		var setText = "";
		for (var i = 0; i < tests.list.length;i++) {
			if (i > 0)
				setText += "----DUSPLIT----";
				
			//alert("Final: [" + suid + "]\n" + tests.list[i].name);
			setText += tests.list[i].uid+"**ITEMSPLIT**";
			setText += tests.list[i].suid+"**ITEMSPLIT**";
			setText += tests.list[i].name+"**ITEMSPLIT**";
			setText += tests.list[i].appliesto+"**ITEMSPLIT**";
			setText += tests.list[i].js+"**ITEMSPLIT**";
		}
		// delete it
		if (DWfile.exists(fname)) {
			DWfile.remove(fname);
		}
		
		// now write it fresh
		DWfile.write(fname, setText);		
	},
	deserializeTestsFromWeb: function(uid) {
		if (remoteData.interpretServerBoolean(remoteData.requestData(ws.VALIDATECONNECTION, {})) == true) {
			var rawFileData = remoteData.requestData(ws.DOWNLOADTESTS, {'uid':uid});
			tests.list = [];
			var itemList = rawFileData.split("----DUSPLIT----");
			for (var i = 0; i < itemList.length; i++) {
				var iObj = itemList[i].split("**ITEMSPLIT**");
				tests.list[tests.list.length] = tests.createTestObject(iObj[0],iObj[1],iObj[2],iObj[3]);
				
				try {tests.list[tests.list.length-1].js = iObj[4];} catch(e) {}
				try {tests.list[tests.list.length-1].appliestoroot = iObj[5];
					//alert("Replacing " + tests.list[tests.list.length-1].appliesto);
					var rootfolder = site.siteRelativeToLocalPath("/");
					var isWindows = utils.isWindows();

					var folderChar = "/";
					if (isWindows == false)
						folderChar = ":";					
					tests.list[tests.list.length-1].appliesto = rootfolder + tests.list[tests.list.length-1].appliesto.replace(tests.list[tests.list.length-1].appliestoroot, "").replace(/\\/g, folderChar).replace(/:/g, folderChar).replace(/\//g, "\\");
					if (tests.list[tests.list.length-1].appliesto.indexOf("//") > -1 || tests.list[tests.list.length-1].appliesto.indexOf("\\\\") > -1) {
						tests.list[tests.list.length-1].appliesto = tests.list[tests.list.length-1].appliesto.replace(rootfolder, "");
					}
					//alert("..with " + tests.list[tests.list.length-1].appliesto);
					} catch(e) {}
			}
	
		}
	},
	deserializeTestsFromFileSystem: function(uid) {
		var fname = tests.getTestsFileLocation(uid);
		if (DWfile.exists(fname)) {
			tests.list = [];
			var rawFileData = DWfile.read(fname);
			//alert("File [" + uid + "]:\n" + rawFileData);
			var itemList = rawFileData.split("----DUSPLIT----");
			for (var i = 0; i < itemList.length; i++) {
				var iObj = itemList[i].split("**ITEMSPLIT**");
				tests.list[tests.list.length] = tests.createTestObject(iObj[0],iObj[1],iObj[2],iObj[3]);
				try {tests.list[tests.list.length-1].js = iObj[4];} catch(e) {}
			}
		}
	},	
	getTestsFileLocation: function(uid) {
		var nuid = uid.replace(/-/g, "");
		return dw.getConfigurationPath() + "/Shared/du/config/tests_" + nuid + ".dat"; 
	},
	createTestObject: function(tuid, tsuid,tname,ato) {
		return {uid:tuid,suid:tsuid,name:tname,appliesto:ato,js:""};
	},
	deleteTestByUID: function(tuid) {
		var newList = [];
		var suid = "";
		for (var i = 0; i < tests.list.length; i++) {
			if (tests.list[i].uid == tuid) {
				suid = tests.list[i].suid;
			} else {
				newList[newList.length] = tests.list[i];
			}
		}
		tests.list = newList;
		var result = remoteData.interpretServerBoolean(remoteData.requestData(ws.DELETETEST, {'uid':tuid}));
		tests.serializeAndWriteTestsToFileSystem(suid);
	},
	originalFileContents:{},
	implementTestOnFile: function(fileref, testjs) {
		tests.removeTestFromFile(fileref);
		//alert("Setting test on:\n" + fileref);
		testjs = "<!-- DREAMUNIT_BEGIN --><scri" + "pt type=\"text/javascript\" src=\"" + tests.jsLocation + "\"></scr" + "ipt>\n<scr" + "ipt type=\"text/javascript\">\n" + testjs + "</s" + "cript><!-- DREAMUNIT_END -->"; 
		var docContents = DWfile.read(fileref);
		tests.originalFileContents[fileref] = docContents;
		docContents = docContents.replace("</body", testjs+"</body");
		DWfile.write(fileref, docContents);
	},
	extractTestResults: function(tuid) {
		var ti = tests.getTestIndexByUID(tuid);
		var theDOM = dw.getDocumentDOM();
		var brow = theDOM.browser.getWindow();
		var browDom = brow.document.body;
		var ihtml = browDom.innerHTML;		
		tests.list[ti].results = brow.document.getElementById("dreamunit_results").innerHTML.split("**DREAMUNIT_ENTRY**");		
	},
	removeTestFromFile: function(fileref) {
		if (DWfile.exists(fileref)) {
			var nTest = tests.originalFileContents[fileref];
			if (nTest) {
				// manually remove the code
				var beginStr = "<!-- DREAMUNIT_BEGIN -->";
				var endStr = "<!-- DREAMUNIT_END -->";
				var beginPos = nTest.indexOf(beginStr);
				var endPos = nTest.indexOf(endStr+endStr.length);
				nTest = nTest.substring(0,beginPos)+nTest.substring(endPos);
				DWfile.write(fileref, 	nTest);	
			}
				
		}		
	}
}